//验证上传图片
function customCheckData(file) {
    var filevalue = file.name;
    var msg="";
    if (/[^\x00-\xff]/g.test(filevalue)){
        msg="文件名中不能含有汉字";
    }
    else if(checkStrT(filevalue)){//检验不能有非常字符
        msg="文件名中不能含有非法字符";
    }
    else if(filevalue.length > 30){//检验文件名的长度
        msg="文件名过长";
    }

    return msg;
}

function browseBtnDisable(){
    $("#browseBtn_img").attr("src", baseContextPath+"static/images/browse_active.png");
}

var initHandler = function (up, params) {
    if($("#type").val() == "toView"){
        up.disableBrowse();
        browseBtnDisable();
        $("#browseBtn").attr("disabled", "disabled");
        $("#uploadBtn").attr("disabled","disabled");
    }
};

var filesAddedHandler = function (up, files) {
    var txtFileName = "";
    $.each(files, function (i, file) {
        txtFileName+=file.name+";";
    });
    $("#txtFileName").val(txtFileName);
    up.refresh(); // Reposition Flash/Silverlight
};



var fileFilteredHandler = function (up,file) {
    var msg=customCheckData(file);
    if(msg != ""){
        up.trigger('Error', {
            code : -800,
            message : msg,
            file : file
        });
        return false;
    }

    up.refresh(); // Reposition Flash/Silverlight
};

var beforeUploadHandler=function (up,file){
    file.target_name=$("#fileName").val();

    up.refresh(); // Reposition Flash/Silverlight
};

var uploadProgressHandler = function (up, file) {
    try {
        var percent = file.percent;
        var progress = new FileProgress(file, progress_target);
        progress.setProgress(percent);
        progress.setStatus("上传中...");
    } catch (e) {
    }
};


var defaultFile={id:"singlefile",name:"singlefile"};
var errorHandler = function (up, err) {
    try {
        var file = err.file === undefined ? defaultFile: err.file;
        var progress = new FileProgress(file, progress_target);
        progress.setError();

        switch (err.code){
            case plupload.GENERIC_ERROR :
                progress.setStatus("文件上传期间报错。请稍后再试!");
                break;
            case plupload.HTTP_ERROR :
                progress.setStatus("文件上传失败，服务端报错，请稍后再试!");
                break;

             case   plupload.IO_ERROR :
             case   plupload.SECURITY_ERROR :
                 progress.setStatus("文件读取失败，请确保您有权限访问该文件或文件没有被其他程序打开!")
                 break;
            case plupload.INIT_ERROR:
                progress.setStatus("文件上传控件初始化错误，请刷新页面重试!");
                break;
            case plupload.FILE_SIZE_ERROR:
                progress.setStatus("文件过大，上传文件不能超过"+fileSizeLimit);
                break;
            case plupload.FILE_EXTENSION_ERROR:
                progress.setStatus("文件格式不符要求，请选择其他文件!");
                break;
            case plupload.FILE_DUPLICATE_ERROR:
                progress.setStatus("文件已经在上传队列中，请不要重复上传!");
                break;
            case plupload.IMAGE_FORMAT_ERROR:
            case plupload.IMAGE_MEMORY_ERROR:
            case plupload.IMAGE_DIMENSIONS_ERROR:
                progress.setStatus("文件格式不符要求，请选择其他文件!");
                break;
            case -800:
                progress.setStatus(err.message);
                break;
            default:
                progress.setStatus("文件上传期间报错。请稍后再试!");
                break;

        }
    } catch (e) {
    }
    up.refresh(); // Reposition Flash/Silverlight
};


function hasAuthoritys(ruleStr,authority){
    if(ruleStr == "")
        return false;
    var rules=ruleStr.split(",");
    for(var i = 0; i < rules.length; i++){
        if(rules[i] == authority)
            return true;
    }
    return false;
}


function showPicture(stu){
    //清空信息
    $("#show").children().remove();
    var str = "";
    if(stu == ""){
        str = str + "<tr><td><label style='color: red;'><h3>没有图片</h3></label></td></tr>";
    }else{
        var strNa = "";//图片名字
        var strNaAll = "";

        strNa = stu.substring(stu.lastIndexOf("/") + 1, stu.length);   //获取图片名字
        var strPath =remoteAddress + stu+"?rand="+new Date().getTime(); //图片完整路径
        str += "<tr align='center'>";
        strNaAll = "<tr>";
        var picpath = stu;
        str = str + "<td><a class='pic_a' href='" + strPath + "' rel='lightbox'><img width='150' height='150' border='0' src = '" + strPath + "' /></a></td>";
        strNaAll =  strNaAll + "<td align='center'>" + strNa + "&nbsp;";


        strNaAll =  strNaAll + "</td>";

        str += "</tr>";
        strNaAll = strNaAll + "</tr>";
        str = str + strNaAll;
        strNaAll = "";

    }
    $("#show").html(str);
    $(".pic_a").lightBox();
}


//上传图片完毕后执行
function showResponse(serverData)  {
    var responseData = $(serverData.response);
    if(responseData.find("span[name=result]").text() == "success"){
        var url=responseData.find("span[name=url]").text();
        alert("上传成功");
        if (typeof(window.opener.uploadcallback)!="undefined" && typeof(window.opener.uploadcallback)!="unknown"){
            window.opener.uploadcallback($("#uploadType").val(),responseData.find("span[name=url]").text(),$("#keyId").val());
        }
        showPicture(url);
        //window.close();

    }else(
        alert("上传失败"+responseData.find("span[name=msg]").text())
        )
}


var fileUploadedHandler = function (up, file,serverData) {
    if (serverData === " ") {
    } else {
        var progress = new FileProgress(file, progress_target);
        try {
            showResponse(serverData);
            progress.setComplete();
            progress.setStatus("文件上传成功.");
        } catch (e) {
            progress.setError();
            progress.setStatus(e.message);
        }
    }
};

var uploadCompleteHandler = function () {
    $("#txtFileName").val("");
};